<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notepad</title>
    <style>
        .back {
            position: absolute;
        }
    </style>

    <!-- mui -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css">

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- bootstrap-validate -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css">

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- city-picker 省市区选择器 -->
    <link rel="stylesheet" href="./assets/plugins/city-picker/city-picker.css">

    <!-- 重置样式 -->
    <link rel="stylesheet/less" href="./assets/css/reset.less" />

    <!-- 公共样式 -->
    <link rel="stylesheet/less" href="./assets/css/common.less" />

    <!-- 页面独立的 -->
    <link rel="stylesheet/less" href="./assets/css/UserProfile.less" />
</head>

<body>
    <!-- 用户信息 -->
    <div class="back">
        <img src="./assets/images/back.svg" alt="">
    </div>
    <div class="user">
        <div class="avatar wow fadeInUp">
            <img src="./assets/images/avatar.png" />
        </div>
    </div>

    <form class="profile wow fadeInUp" data-wow-delay="200ms" id="profile">
        <div class="form-group">
            <label for="nickname">姓名</label>
            <input type="text" class="form-control" id="nickname" name="nickname" placeholder="请输入姓名" />
        </div>

        <div class="form-group">
            <label for="phone">手机号</label>
            <input type="text" class="form-control" id="phone" name="phone" placeholder="请输入手机号" />
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" id="password" placeholder="不修改密码请留空" />
        </div>

        <div class="form-group">
            <label for="gender">性别</label>
            <select class="form-control" id="gender">
                <option value="0">保密</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>

        <div class="form-group region">
            <label for="region">选择地区</label>
            <input type="text" class="form-control" id="region" data-toggle="city-picker" readonly
                placeholder="请选择地区" />
        </div>

        <div class="form-group region">
            <label for="address">详细地址</label>
            <input type="text" class="form-control" id="address" placeholder="请输入详细地址" />
        </div>

        <!-- 头像上传文件框 -->
        <input type="file" name="avatar" id="avatar">

        <div class="action">
            <button class="btn">提交</button>
        </div>
    </form>
</body>

</html>
<!-- 解析less的文件 -->
<script src="./assets/js/less.min.js"></script>

<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>

<!-- mui -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- bootstrap-validator -->
<script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>

<!-- city-picker -->
<script src="./assets/plugins/city-picker/city-picker.data.min.js"></script>
<script src="./assets/plugins/city-picker/city-picker.min.js"></script>

<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- axios -->
<script src="./assets/plugins/axios/axios.min.js"></script>

<!-- 用axios封装的request -->
<script src="./assets/plugins/axios/request.js"></script>

<!-- cookie -->
<script src="./assets/plugins/cookie.js"></script>

<!-- helper -->
<script src="./assets/plugins/helper.js"></script>

<script>
    //实例化wow.js
    new WOW().init()

    // 检查是否已经登录
    checkLogin()

    // 获取cookie
    let business = cookie.get('business') ? JSON.parse(cookie.get('business')) : {};
    console.log(business);
    // 把已有的数据回显输入框
    (() => {
        if (Object.getOwnPropertyNames(business).length > 0) {
            $('.avatar img').attr('src', business.avatar)
            $('#nickname').val(business.nickname)
            $('#phone').val(business.mobile)
            $('#gender').val(business.gender)
            // 地区
            $('#region').citypicker({
                province: business.province,
                city: business.city,
                district: business.district
            })
            $('#address').val(business.address)
        }
    })();

    // 点击头像拉起上传框
    $('.avatar').click(() => {
        $('#avatar').click()
    })

    // 头像预览
    $('#avatar').change(function () {
        // 获取用户选择的图片
        let file = this.files[0] ? this.files[0] : ''

        // 把file转成base64格式
        let Reader = new FileReader()
        Reader.readAsDataURL(file)
        Reader.onload = function () {
            $('.avatar img').attr('src', Reader.result)
        }
    })

    // 实例化bootstrap表单验证插件
    $('#profile').bootstrapValidator({
        // 填写配置项
        message: '表单内容不能为空',// 所有字段默认错误信息
        // 配置三种状态的图标 验证成功 验证成功 验证中
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        // 设置需要验证字段
        fields: {
            phone: {
                // 自定义验证器
                validators: {
                    // 设置该字段不能为空
                    notEmpty: {
                        message: '手机号不能为空'
                    },
                    // 设置该字段的长度
                    stringLength: {
                        min: 11,// 最小值
                        max: 11,// 最大值
                        message: '手机号必须11位'
                    },
                    // 手机号正则  输入手机号必须符合当前规则
                    regexp: {
                        // 19 \d 表示0到9之间的任何一个数字
                        regexp: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
                        message: '手机号错误'
                    }
                }
            },
            nickname: {
                validators: {
                    notEmpty: {
                        message: '姓名不能为空'
                    }
                }
            }
        }
    }).on('success.form.bv', function (e) {
        // 阻止多次提交
        e.preventDefault();
    });

    // 表单提交事件
    $('#profile').on('submit', async function () {
        // 获取表单的某个属性
        let bootstrapValidators = $('#profile').data('bootstrapValidator');

        // 判断输入内容是否验证失败
        if (!bootstrapValidators.isValid()) {
            // 验证失败就不执行后面代码
            return false;
        }

        // 获取需要上传的数据
        let params = {
            id: business.id,
            nickname: $.trim($('#nickname').val()),
            mobile: $.trim($('#phone').val()),
            gender: $.trim($('#gender').val()),
            address: $.trim($('#address').val()),
        }

        // 获取输入密码
        let password = $.trim($('#password').val());

        // 如果修改密码就把密码追加请求参数里
        if (password) {
            params.password = password;
        }

        // 获取地区码
        let citypicker = $('#region').data("citypicker");
        let code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");

        if (code) {
            params.region = code;
        }

        // 获取选择的图片
        let avatar = $('#avatar')[0].files[0];

        if (avatar) {
            params.avatar = avatar;
        }

        // 上传数据，因为要上传文件所以用UPLOAD
        let result = await UPLOAD({
            url: 'business/profile',
            params
        })

        if (result.code === 1) {
            mui.toast(result.msg);

            // 更新cookie
            cookie.set('business', JSON.stringify(result.data));

            // 跳转回个人页面
            setTimeout(() => {
                location.href = 'UserIndex.html';
            }, 2000)
        } else {
            // 提交失败 返回错误信息
            mui.toast(result.msg);
        }
    })

    // 返回按钮
    $('.back').click(() => {
        history.back()
    })


</script>